package com.xncoding.pos.controller;

import io.github.biezhi.ome.OhMyEmail;
import io.github.biezhi.ome.SendMailException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.TaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import static io.github.biezhi.ome.OhMyEmail.SMTP_QQ;

@RestController
@Slf4j
public class QQMailController {

    /**
     * 该邮箱修改为你需要测试的邮箱地址 即接收方的邮箱地址
     */
    private static final String TO_EMAIL = "guoyibo@ctfo.com";

    @Value("${spring.mail.username}")
    private String defaultFrom;

    // @PostConstruct
    private void initMailConfig() {
        // 配置，一次即可 your@password不是指QQ邮箱的密码，是指QQ邮箱的授权码
        OhMyEmail.config(SMTP_QQ(false), "767062594@qq.com", "alrhpeudakhobdcc");
    }

    @GetMapping("testSendText")
    public String testSendText() throws SendMailException {
        OhMyEmail.subject("这是一封测试TEXT邮件")
                .from(defaultFrom)
                .to(TO_EMAIL)
                .text("信件内容")
                .send();
        return "发送成功";
    }

    @GetMapping("testSendHtml")
    public String testSendHtml() throws SendMailException {
        OhMyEmail.subject("这是一封测试HTML邮件")
                .from(defaultFrom)
                .to(TO_EMAIL)
                .html("<h1 font=red>信件内容</h1>")
                .send();
        return "发送成功";
    }

    @Autowired
    private TaskExecutor taskExecutor;


    @GetMapping("testSendAsync")
    public String testSendAsync() {

        taskExecutor.execute(() -> {
            try {
                OhMyEmail.subject("这是一封测试邮件")
                        .from("测试异步发送邮件")
                        .to(TO_EMAIL)
                        .html("<h1 font=red>信件内容</h1>")
                        .send();
            } catch (SendMailException e) {
                log.error(e.getMessage(), e);
            }
            log.info("email: {} send success", TO_EMAIL);
        });
        return "发送成功";
    }

    @GetMapping("testSendAttach")
    public String testSendAttach(@RequestParam String filePath) throws SendMailException {
        OhMyEmail.subject("这是一封测试附件邮件")
                .from(defaultFrom)
                .to(TO_EMAIL)
                .html("<h1 font=red>信件内容</h1>")
                .attach(new File(filePath), "测试图片.jpg")
                .send();
        return "发送成功";
    }

    @GetMapping("testSendAttachURL")
    public String testSendAttachURL() throws SendMailException {
        try {
            OhMyEmail.subject("这是一封测试网络资源作为附件的邮件")
                    .from(defaultFrom)
                    .to(TO_EMAIL)
                    .html("<h1 font=red>信件内容</h1>")
                    .attachURL(new URL("https://avatars1.githubusercontent.com/u/2784452?s=40&v=4"), "测试图片.jpg")
                    .send();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return "发送成功";
    }

}
